package io.github.talelin.latticy.controller.v1;


import com.baomidou.mybatisplus.core.metadata.IPage;
import io.github.talelin.latticy.common.util.PageUtil;
import io.github.talelin.latticy.dto.SpecKeyDTO;
import io.github.talelin.latticy.service.SpecKeyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.github.talelin.latticy.model.SpecKeyDO;
import io.github.talelin.latticy.vo.CreatedVO;
import io.github.talelin.latticy.vo.DeletedVO;
import io.github.talelin.latticy.vo.PageResponseVO;
import io.github.talelin.latticy.vo.UpdatedVO;

import javax.validation.constraints.Min;
import javax.validation.constraints.Max;
import javax.validation.constraints.Positive;

import java.util.List;

/**
* @author generator@TaleLin
* @since 2021-06-19
*/
@RestController
@RequestMapping("/v1/spec-key")
@Validated
public class SpecKeyController {

    @Autowired
    private SpecKeyService specKeyService;


    @PostMapping("")
    public CreatedVO create(@RequestBody @Validated SpecKeyDTO specKeyDTO) {

        this.specKeyService.saveSpecKey(specKeyDTO);
        return new CreatedVO();
    }

    @PutMapping("/{id}")
    public UpdatedVO update(@PathVariable @Positive(message = "{id.positive}") Long id,
                            @RequestBody @Validated SpecKeyDTO specKeyDTO) {
        this.specKeyService.update(id,specKeyDTO);
        return new UpdatedVO();
    }

    @DeleteMapping("/{id}")
    public DeletedVO delete(@PathVariable @Positive(message = "{id.positive}") Long id) {
        this.specKeyService.deleteSpecKey(id);

        return new DeletedVO();
    }

    @GetMapping("/{id}")
    public SpecKeyDO getDetail(@PathVariable(value = "id") @Positive(message = "{id.positive}") Long id) {
        return this.specKeyService.getById(id);
    }

    @GetMapping("/by/spu/{id}")
    public List<SpecKeyDO> getBySpuId(@PathVariable(value = "id") @Positive(message = "{id.positive}") Long spuId){
        return specKeyService.getBySpuId(spuId);
    }

    @GetMapping("/page")
    public PageResponseVO<SpecKeyDO> page(
            @RequestParam(name = "count", required = false, defaultValue = "10")
            @Min(value = 1, message = "{page.count.min}")
            @Max(value = 30, message = "{page.count.max}") Long count,
            @RequestParam(name = "page", required = false, defaultValue = "0")
            @Min(value = 0, message = "{page.number.min}") Long page
    ) {
        IPage<SpecKeyDO> paging =  this.specKeyService.getPage(count,page);
        return PageUtil.build(paging);
    }

}
